﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FireStarterModeller.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI.Class
{
    public class ShowErrors : FormWin
    {
        private readonly DMSolution _solution;
        private readonly IControlFactory _controlFactory;
        private readonly ErrorViewerControl _errorViewerControl;
        private readonly IButtonGroupControl _buttonGroupControl;
        public IButton CancelationButton { get; private set; }
        public IButton OkButton { get; private set; }

        public ShowErrors(DMSolution solution,IControlFactory controlFactory)
        {
            if (solution == null) throw new ArgumentNullException("solution");
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            _solution = solution;
            _controlFactory = controlFactory;
            string headingText = "You cannot generate for '" + solution.ToString() + "' because the following rules are broken.";
            string descriptionText = "Please fix these prior to generating for '" + solution.ToString() + "'";
            
            _errorViewerControl = new ErrorViewerControl { ErrorList = solution.Validate() };
            IUserControlHabanero mainPanel = this.ControlFactory.CreateUserControl();
            IPanel panel = this.ControlFactory.CreatePanel();
            _buttonGroupControl = this.ControlFactory.CreateButtonGroupControl();
            OkButton = ButtonGroupControl.AddButton("OK", (sender, args) => CloseThis(DialogResult.OK));
            CancelationButton = ButtonGroupControl.AddButton("Cancel", (sender, args) => CloseThis(DialogResult.Cancel));
            ButtonGroupControl.AddButton("ReValidate",RevalidateSolution);

            ColumnLayoutManager columnLayoutManager = new ColumnLayoutManager(panel, this.ControlFactory) { ColumnCount = 1 };
            columnLayoutManager.AddControl(this.ControlFactory.CreateLabel(headingText, true));
            columnLayoutManager.AddControl(this.ControlFactory.CreateLabel(descriptionText, true));
            panel.Height = 50;
            
            BorderLayoutManager layoutManager = this.ControlFactory.CreateBorderLayoutManager(mainPanel);
            layoutManager.AddControl(panel, BorderLayoutManager.Position.North);
            layoutManager.AddControl(ErrorViewerControl, BorderLayoutManager.Position.Centre);
            layoutManager.AddControl(ButtonGroupControl, BorderLayoutManager.Position.South);

            Width = 800;
            Height = 600;
            BorderLayoutManager formLayoutManager = this.ControlFactory.CreateBorderLayoutManager(this);
            formLayoutManager.AddControl(mainPanel, BorderLayoutManager.Position.Centre);
        }

        private void CloseThis(DialogResult dialogResult)
        {
            this.DialogResult = dialogResult;
            Close();
        }

        private void RevalidateSolution(object sender, EventArgs args)
        {
            ErrorViewerControl.ErrorViewerGrid.Rows.Clear();
            ErrorViewerControl.ErrorList = _solution.Validate();
        }

        public IControlFactory ControlFactory
        {
            get { return _controlFactory; }
        }

        public ErrorViewerControl ErrorViewerControl
        {
            get { return _errorViewerControl; }
        }

        public IButtonGroupControl ButtonGroupControl
        {
            get { return _buttonGroupControl; }
        }
    }
}
